#include "elradio.h"
#include <QPaintEvent>
#include <QPainter>
#include "../basic/eltheme.h"

ElRadio::ElRadio(QWidget *parent)
    : ElAbstractButton{parent}
{
    _circle = QRectF(10, 8, 15, 15);
    initPalette();
}

ElRadio::ElRadio(const QString &text, QWidget *parent)
    : ElRadio{parent}
{
    setText(text);
}

bool ElRadio::checked() const
{
    return _checked;
}

void ElRadio::setChecked(bool checked)
{
    if (_checked == checked)
        return;
    _checked = checked;
    emit toggled(_checked);
    update();
}

bool ElRadio::hasBorder() const
{
    return _border;
}

void ElRadio::setBorder(bool b)
{
    _border = b;
    update();
}

bool ElRadio::event(QEvent *e)
{
    if (e->type() == QEvent::EnabledChange) {
        _palette.toggleColorGroup(isEnabled() ? ElPalette::Normal : ElPalette::Disabled);
        update();
    }
    return ElAbstractButton::event(e);
}

void ElRadio::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    if (_border) // draw outer border
        drawBorder(&painter);
    painter.setPen(_palette.color(ElPalette::Border));
    painter.setBrush(_palette.color(ElPalette::Background));
    painter.drawEllipse(_circle.center(), _circle.width() / 2, _circle.height() / 2);
    QColor fgColor = ThemeColor::primaryColor;
    if (_checked) {
        drawChecked(&painter);
        if (!isEnabled())
            fgColor = ThemeColor::primaryDisabledColor;
    } else {
        fgColor = _palette.color(ElPalette::Foreground);
    }
    painter.setPen(fgColor);
    QRectF drawingRect{_circle.right() + 5, _circle.top(), 10, _circle.height()}, br;
    painter.drawText(drawingRect, Qt::AlignVCenter | Qt::AlignLeft, _text, &br);
    resize(_circle.left() + br.right(), height());
    // painter.eraseRect(drawingRect);
    drawingRect = QRectF{_circle.right() + 5, _circle.top(), br.width(), _circle.height()};
    painter.drawText(drawingRect, Qt::AlignVCenter | Qt::AlignLeft, _text);
    e->accept();
}

void ElRadio::enterEvent(QEnterEvent *e)
{
    if (!isEnabled()) {
        e->accept();
        return;
    }
    showTooltip();
    _palette.toggleColorGroup(ElPalette::Entered);
    update();
    e->accept();
}

void ElRadio::leaveEvent(QEvent *e)
{
    if (!isEnabled()) {
        e->accept();
        return;
    }
    hideTooltip();
    _palette.toggleColorGroup(ElPalette::Normal);
    update();
    e->accept();
}

void ElRadio::mousePressEvent(QMouseEvent *e)
{
    if (!isEnabled()) {
        e->accept();
        return;
    }
    _palette.toggleColorGroup(ElPalette::Pressed);
    update();
    emit pressed();
    e->accept();
}

void ElRadio::mouseReleaseEvent(QMouseEvent *e)
{
    if (!isEnabled()) {
        e->accept();
        return;
    }
    _palette.toggleColorGroup(ElPalette::Entered);
    emit released();
    auto pos = e->pos();
    if (pos.x() >= 0 && pos.y() >= 0 && pos.x() <= width() && pos.y() <= height()) {
        emit clicked();
        if (!_checked) {
            _checked = true;
            emit toggled(_checked);
        }
    }
    update();
    e->accept();
}

void ElRadio::initPalette()
{
    // background is default color, which is white.
    _palette.setColor(ElPalette::Border, ThemeColor::lineColor);
    _palette.setColor(ElPalette::Foreground, ThemeColor::textPrimaryColor);

    _palette.toggleColorGroup(ElPalette::Entered);
    _palette.setColor(ElPalette::Border, ThemeColor::lineColor);
    _palette.setColor(ElPalette::Foreground, ThemeColor::textPrimaryColor);

    _palette.toggleColorGroup(ElPalette::Pressed);
    _palette.setColor(ElPalette::Background, ThemeColor::primaryShallowColor);
    _palette.setColor(ElPalette::Border, ThemeColor::primaryColor);
    _palette.setColor(ElPalette::Foreground, ThemeColor::textPrimaryColor);

    _palette.toggleColorGroup(ElPalette::Disabled);
    _palette.setColor(ElPalette::Border, ThemeColor::lineDisabledColor);
    _palette.setColor(ElPalette::Foreground, ThemeColor::textDisabledColor);
    _palette.toggleColorGroup(ElPalette::Normal);
}

void ElRadio::drawBorder(QPainter *painter)
{
    QPen pen;
    if (_checked) {
        pen.setColor(isEnabled() ? ThemeColor::primaryColor : ThemeColor::primaryDisabledColor);
    } else {
        pen.setColor(isEnabled() ? ThemeColor::lineColor : ThemeColor::lineDisabledColor);
    }
    painter->setPen(pen);
    painter->setBrush(Qt::white);
    painter->drawRoundedRect(getDrawRect(), height() / 5, height() / 5);
}

void ElRadio::drawChecked(QPainter *painter)
{
    QColor fillColor = ThemeColor::primaryColor;
    if (!isEnabled())
        fillColor = ThemeColor::primaryDisabledColor;
    painter->setBrush(fillColor);
    painter->drawEllipse(_circle.center(), _circle.width() / 2, _circle.height() / 2);
    painter->setBrush(Qt::white);
    painter->drawEllipse(_circle.center(), _circle.width() / 8, _circle.height() / 8);
}
